package com.houseliker.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;

import com.google.gson.Gson;
import com.houseliker.listener.OnArticleSelectedListener;
import com.houseliker.listener.OnHeaderClickListener;
import com.houseliker.view.IHeaderLayout;

/**
 * Created by sunrx on 2015/12/7.
 */
public abstract class BaseFragment extends Fragment {
    protected Activity mContext;
    protected OnArticleSelectedListener mListener;
    protected Gson gson;

    protected View contentView;

    private IHeaderLayout headerLayout;

    protected static final int TYPE_NORMAL = 0;
    protected static final int TYPE_HEADER = 1;

    protected BaseFragment() {
        mContext = getActivity();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext = activity;
        try {
            mListener = (OnArticleSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(
                    "Activity must implement OnArticleSelectedListener");
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gson = new Gson();
        if (null == mContext) {
            mContext = getActivity();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    protected View setView(LayoutInflater inflater, int layoutResId, int type) {
        switch (type) {
            case TYPE_NORMAL:
                contentView = inflater.inflate(layoutResId, null);
                break;
            default:
                headerLayout = new IHeaderLayout(mContext, inflater, layoutResId, type);
                break;
        }
        if (null != headerLayout) {
            headerLayout.setHeaderClickListener(onHeaderClickListener);
            return headerLayout;
        }
        return contentView;
    }

    private OnHeaderClickListener onHeaderClickListener = new OnHeaderClickListener() {
        @Override
        public void onClickLeftButton() {
            onLeftButtonClick();
        }

        @Override
        public void onClickRightButton() {
            onRightButtonClick();
        }

        @Override
        public void onClickReLoadButton() {
            onReLoadButtonClick();
        }
    };

    protected void onLeftButtonClick() {
        //子类选择性实现
    }

    protected void onRightButtonClick() {
        //子类选择性实现
    }

    protected void onReLoadButtonClick() {
        //子类选择性实现
    }

    /**
     * 显示“返回”按钮和title
     *
     * @param titleText
     */
    public void setHeaderBar(String titleText) {
        if (null != headerLayout) {
            headerLayout.setHeaderBar(titleText);
        }
    }

    /**
     * 显示“返回”按钮、title、右边按钮。
     *
     * @param titleText
     * @param rightText
     */
    public void setHeaderBar(String titleText, String rightText) {
        if (null != headerLayout) {
            headerLayout.setHeaderBar(titleText, rightText);
        }
    }

    /**
     * 显示左边按钮、title、右边按钮
     *
     * @param leftText
     * @param titleText
     * @param rightText
     */
    public void setHeaderBar(String leftText, String titleText, String rightText) {
        if (null != headerLayout) {
            headerLayout.setHeaderBar(leftText, titleText, rightText);
        }
    }

    /**
     * 隐藏左边箭头
     */
    public void hideHeaderLeftArrow() {
        if (null != headerLayout) {
            headerLayout.hideHeaderLeftArrow();
        }
    }

    /**
     * 隐藏左边按钮
     */
    public void hideHeaderLeftButtom() {
        if (null != headerLayout) {
            headerLayout.hideHeaderLeftButtom();
        }
    }

}
